#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;
const int Mod = 998244353;
int qpow(int a, int b)
{
    int ret = 1;
    while (b > 0)
    {
        if (b & 1)
            ret = ret * a % Mod;
        a = a * a % Mod;
        b >>= 1;
    }
    return ret;
}

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a.begin(), a.end());
    int ans1 = 0, ans2 = 0;
    int med1 = a[floor((n - 1) / 2.0)], med2 = a[floor((n - 1) / 2.0) + 1];
    for (int i = 0; i < n - 1; i++) ans1 += abs(a[i] - med1);
    for (int i = 1; i < n; i++) ans2 += abs(a[i] - med2);
    cout << min(ans1, ans2) << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}